﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ecommerce.Model;

namespace Ecommerce.Admin.Models
{
    public class ArticleCategoryData
    {
        public int ID { get; set; }
        public string CategoryName { get; set; }
        public int? ParentId { get; set; }
        public int? Priority { get; set; }
        public bool? Status { get; set; }
        public string TreeNode { get; set; }
        public string ImagePath { get; set; }
    }
    public class ArticleCategoryViewModel
    {
        public ArticleCategory Category { get; set; }
        public string Categories { get; set; }
        public IList<ArticleCategoryData> TreeCategories { get; set; }
        public string ImagePath { get; set; }
    }
    public class ArticleCategoryDataModel
    {
        TMDTDataContext db = new TMDTDataContext();
        public string MenuTree(int parentId, IList<ArticleCategory> categories, int? idCheck)
        {
            return MenuTree(parentId, categories, "", "└---", idCheck);
        }
        public string MenuTree(int parentId, IList<ArticleCategory> categories, string res, string sep, int? idCheck)
        {
            foreach (var item in categories)
            {
                if (item.ParentID == parentId)
                {
                    string re = "";
                    if (item.ID == idCheck)
                    {
                        re = "<option selected='selected' value='" + item.ID + "'>" + sep + item.Title + "</option>";
                    }
                    else
                    {
                        re = "<option value='" + item.ID + "'>" + sep + item.Title + "</option>";
                    }

                    res += MenuTree(item.ID, categories, re, sep + "------", idCheck);
                }
            }
            return res;
        }

        public string MenuTreeList(int parentID, IList<ArticleCategory> categories, List<int?> idCheck)
        {
            return MenuTreeList(parentID, categories, "", "└---", idCheck);
        }
        public string MenuTreeList(int parentID, IList<ArticleCategory> categories, string res, string sep, List<int?> idCheck)
        {
            foreach (var item in categories)
            {
                if (item.ParentID == parentID)
                {
                    string re = "";
                    if (idCheck.Contains(item.ID))
                    {
                        re = "<option selected='selected' value='" + item.ID + "'>" + sep + item.Title + "</option>";
                    }
                    else
                    {
                        re = "<option value='" + item.ID + "'>" + sep + item.Title + "</option>";
                    }

                    res += MenuTreeList(item.ID, categories, re, sep + "------", idCheck);
                }
            }
            return res;
        }

        public IList<ArticleCategoryData> MenuTreeNode(int parentId, IList<ArticleCategory> categories)
        {
            return MenuTreeNode(parentId, categories, "", "");
        }
        public IList<ArticleCategoryData> listTree = new List<ArticleCategoryData>();
        public IList<ArticleCategoryData> MenuTreeNode(int parentId, IList<ArticleCategory> categories, string res, string sep)
        {
            foreach (var item in categories)
            {
                if (item.ParentID == parentId)
                {
                    string re = "<a href='/ArticleCategory/Edit/" + item.ID + "'>" + sep + item.Title + "</a>";
                    res += MenuTreeNode(item.ID, categories, re, sep + "------");
                    var data = new ArticleCategoryData()
                    {
                        ID = item.ID,
                        CategoryName = item.Title,
                        ParentId = item.ParentID,
                        Status = item.Status,
                        Priority = item.Priority,
                        ImagePath = item.ImagePath,
                        TreeNode = re
                    };
                    listTree.Add(data);
                }
            }
            return listTree;

        }
        public IList<string> NodeToDelete(int parentId, IList<ArticleCategory> categories)
        {
            return NodeToDelete(parentId, categories, "");
        }
        public IList<string> listNode = new List<string>();
        public IList<string> NodeToDelete(int parentId, IList<ArticleCategory> categories, string res)
        {
            foreach (var item in categories)
            {
                if (item.ParentID == parentId)
                {
                    string re = "" + item.ID;
                    res += NodeToDelete(item.ID, categories, re);
                    listNode.Add("" + item.ID);
                }
            }
            return listNode;
        }

    }
}
